package com.bootdo.clouddo.controller.book;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bootdo.clouddo.redis.CommonRedisService;
import com.bootdo.clouddo.redis.RedisEnum;
import com.bootdo.clouddo.task.BookTask;
import com.bootdo.clouddo.utils.StringUtils;
import com.bootdo.clouddo.utils.book.BookEnum;
import com.bootdo.clouddo.utils.book.BookUtils;
import com.bootdo.clouddo.utils.book.BookApi;
import com.bootdo.clouddo.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/book/v1")
public class HomeController {

    @Autowired
    private CommonRedisService redisService;

    @Autowired
    private BookTask bookTask;

    @GetMapping("/getBanners")
    public R getBanners(@RequestParam(required = false, defaultValue = "female") String type) {
        try {
            Optional op = redisService.getAsOptional(RedisEnum.BOOK_BANNER.getKey() + type);
            if(op.isPresent()) {
                return R.data(op.get());
            }
            JSONObject resultVo = BookUtils.fetchJSONObject(BookApi.HOME_GIRL.getUrl());
            if(resultVo != null) {
                JSONArray data = resultVo.getJSONArray("banner");
                if(data.isEmpty()) {
                    return R.error();
                }
                // 保存mysql
                bookTask.saveDetailArr(data);
                redisService.put(RedisEnum.BOOK_BANNER.getKey() + type, data, RedisEnum.BOOK_BANNER.getExpire());
                return R.data(data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/getRecommandList")
    public R getRecommandList(@RequestParam(required = false, defaultValue = "female") String type) {
        try {
            Optional op = redisService.getAsOptional(RedisEnum.BOOK_DASHBOARD.getKey() + type);
            if(op.isPresent()) {
                return R.data(op.get());
            }
            JSONObject resultVo = BookUtils.fetchJSONObject(StringUtils.equals(type, BookEnum.MALE.getKey()) ? BookApi.HOME_BOY.getUrl() : BookApi.HOME_GIRL.getUrl());
            if(resultVo != null) {
                JSONArray data = resultVo.getJSONArray("info");
                if(data.isEmpty()) {
                    return R.error();
                }
                // 保存mysql
                bookTask.saveDetailArr(data);
                // 保存redis
                redisService.put(RedisEnum.BOOK_DASHBOARD.getKey() + type, data, RedisEnum.BOOK_DASHBOARD.getExpire());
                return R.data(data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/getRecommandList/{key}")
    public R getRecommandList(@RequestParam(required = false, defaultValue = "female") String type, @PathVariable String key) {
        try {
            String url = StringUtils.equals(type, BookEnum.MALE.getKey()) ? BookApi.HOME_BOY_KEY.getUrl() : BookApi.HOME_GIRL_KEY.getUrl();
            JSONArray data = BookUtils.fetchJSONArray(url + key);
            // 保存mysql
            bookTask.saveDetailArr(data);
            return R.data(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

}
